package com.example.software.service.impl;

import com.example.software.constant.MessageConstant;
import com.example.software.constant.StatusConstant;
import com.example.software.dto.doctorLoginDTO;
import com.example.software.entity.DoctorEntity;
import com.example.software.exception.AccountLockedException;
import com.example.software.exception.AccountNotFoundException;
import com.example.software.exception.PasswordErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.software.utils.PageUtils;
import com.example.software.utils.Query;

import com.example.software.dao.AdminDao;
import com.example.software.entity.AdminEntity;
import com.example.software.service.AdminService;
import org.springframework.util.DigestUtils;


@Service("adminService")
public class AdminServiceImpl extends ServiceImpl<AdminDao, AdminEntity> implements AdminService {

    @Autowired
    private AdminDao adminDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AdminEntity> page = this.page(
                new Query<AdminEntity>().getPage(params),
                new QueryWrapper<AdminEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public AdminEntity login(doctorLoginDTO doctorDTO) {


        String username = doctorDTO.getUsername();
        String password = doctorDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        AdminEntity adminEntity = adminDao.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (adminEntity == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        //对前端传过来的明文密码进行md5加密处理
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(adminEntity.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }


        //3、返回实体对象
        return adminEntity;


    }

}